#include "encoder.h"
#include "head_files.h"

encoder_t encoder_s = {0};
uint8_t key_change_flag = 0;


void encoder_read_value(TIM_HandleTypeDef *htim,encoder_t *encoder)
{
	for(int i = STORAGE_SPACE-1;i > 0;i--)
	{
		encoder->value[i] = encoder->value[i-1];
		encoder->speed[i] = encoder->speed[i-1];
	}
	
	/****************************************/
	//change here when change port
	encoder->value[0] = (int)__HAL_TIM_GetCounter(htim);
	encoder->speed[0] = encoder->value[0] - encoder->value[1];
	encoder->key_enter = (int)HAL_GPIO_ReadPin(key_enter_GPIO_Port, key_enter_Pin);
	encoder->key_quit = (int)HAL_GPIO_ReadPin(key_quit_GPIO_Port, key_quit_Pin);
}


void encoder_state_recognize(encoder_t *encoder)
{
	if(encoder->key_enter == 0)
			encoder->state = CONFIRM;
	else if(encoder->key_quit == 0)
			encoder->state = BACK;
	else if(encoder->speed[0] != 0)
		encoder->state = VALUE;
	else
		encoder->state = NONE;
}
